package com.zzw.springcloud.config;

import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.PredicateSpec;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.function.Function;

@Configuration
public class GatewayRoutesConfig {

    /*
    用配置类的方式配置路由

    - id: member-gateway-route-01
      uri: http://localhost:10000
      predicates:
        - Path=/member/provider/selectById/**
     */
    //@Bean
    //public RouteLocator routes(RouteLocatorBuilder builder) {
    //    RouteLocatorBuilder.Builder routes = builder.routes();
    //
    //    /*
    //    public Builder route(String id, Function<PredicateSpec, Route.AsyncBuilder> fn) {
	//		Route.AsyncBuilder routeBuilder = fn.apply(new RouteSpec(this).id(id));
	//		add(routeBuilder);
	//		return this;
	//	}
    //
    //    1.java中函数式接口的典型用法, 传统的匿名类实现
    //    2.一个函数式接口（Function<PredicateSpec, Route.AsyncBuilder>），用于定义路由规则。
    //    3.接受的是 PredicateSpec，返回的是 Route.AsyncBuilder
    //    4.predicateSpec -> predicateSpec.path("/member/provider/selectById/**").uri("http://localhost:10000"); 就是lambda表达式
    //
	//	（1）PredicateSpec是Gateway提供的路由断言配置接口
    //    （2）Route.AsyncBuilder是Gateway提供的路由构建器, 用于设置路由断言和路由地址
    //    ========================================================================================
	//	Function<PredicateSpec, Route.AsyncBuilder> fn = new Function<PredicateSpec, Route.AsyncBuilder>() {
    //        @Override
    //        public Route.AsyncBuilder apply(PredicateSpec predicateSpec) {
    //            return predicateSpec.path("/member/provider/selectById/**").uri("http://localhost:10000");
    //        }
    //    };
    //    ========================================================================================
    //    //对上面进行简化,lambda表达式的第一种写法
    //    Function<PredicateSpec, Route.AsyncBuilder> fn2 = (predicateSpec) -> {return predicateSpec.path("/member/provider/selectById/**").uri("http://localhost:10000");};
    //    //对上面进行简化,lambda表达式的第二种写法
    //    Function<PredicateSpec, Route.AsyncBuilder> fn3 = predicateSpec -> predicateSpec.path("/member/provider/selectById/**").uri("http://localhost:10000");
    //
    //     */
    //
    //    Function<PredicateSpec, Route.AsyncBuilder> fn = (predicateSpec) -> {return predicateSpec.path("/member/provider/selectById/**").uri("http://localhost:10000");};
    //    Function<PredicateSpec, Route.AsyncBuilder> fn2 = (predicateSpec) -> {return predicateSpec.path("/member/provider/insert").uri("http://localhost:10000");};
    //    Function<PredicateSpec, Route.AsyncBuilder> fn3 = (predicateSpec) -> {return predicateSpec.path("/").uri("http://www.baidu.com");};
    //
    //    RouteLocatorBuilder.Builder route = routes
    //            .route("member-gateway-route-01", predicateSpec -> predicateSpec.path("/member/provider/selectById/**").uri("http://localhost:10000"))
    //            .route("member-gateway-route-02", predicateSpec -> predicateSpec.path("/member/provider/insert").uri("http://localhost:10000"))
    //            .route("member-gateway-route-03", predicateSpec -> predicateSpec.path("/").uri("http://localhost:10000"));
    //
    //    return route.build();
    //}
}
